package com.cn.medicalcare.service.serviceimpl;

import com.cn.medicalcare.customtool.JedisUtil;
import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.customtool.SqlSessionFactoryTool;
import com.cn.medicalcare.mapper.NewsCategoryMapper;
import com.cn.medicalcare.mapper.ProductCategoryMapper;
import com.cn.medicalcare.pojo.AllPage;
import com.cn.medicalcare.pojo.NewsCategory;
import com.cn.medicalcare.pojo.ProductCategory;
import com.cn.medicalcare.service.ProductCategoryService;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class ProductCategoryServiceImpl implements ProductCategoryService {

    private static final String PRODUCT_CATE_CACHE_KEY = "survival_product_category";

    //静态内部类单例模式
    private static class SingletonClassInstance {
        private static final ProductCategoryServiceImpl instance = new ProductCategoryServiceImpl();
    }

    private ProductCategoryServiceImpl() {
    }

    public static ProductCategoryServiceImpl getInstance() {
        return ProductCategoryServiceImpl.SingletonClassInstance.instance;
    }


    @Override
    public List<ProductCategory> selectProductCategoryAll() {
        List<ProductCategory> list = null;
        try {
            String productCateJson = JedisUtil.jedisDo(j -> j.get(PRODUCT_CATE_CACHE_KEY));
            if (StringUtils.isNotEmpty(productCateJson)) {
                JedisUtil.jedisDo(j -> j.expire(PRODUCT_CATE_CACHE_KEY, JedisUtil.CACHE_TIME));
                list = JsonTool.toCollection(productCateJson, new TypeReference<List<ProductCategory>>() {
                });
            }
        } catch (Exception e) {
        }
        if (list == null) {
            list = SqlSessionFactoryTool.DoMapper(ProductCategoryMapper.class, m -> m.selectProductCategoryAll());
            try {
                String productCateJson = JsonTool.toJson(list);
                JedisUtil.jedisDo(j -> j.setex(PRODUCT_CATE_CACHE_KEY, JedisUtil.CACHE_TIME, productCateJson));
            } catch (Exception e) {
            }
        }
        return list;
    }

    @Override
    public List<ProductCategory> selectProductCategoryInPage(AllPage<ProductCategory> allPage) {
        List<ProductCategory> list = SqlSessionFactoryTool.DoMapper(ProductCategoryMapper.class, m -> m.selectProductCategoryInPage(allPage));
        return list;
    }

    @Override
    public int selectProductCategoryCount(ProductCategory productCategory) {
        return SqlSessionFactoryTool.DoMapper(ProductCategoryMapper.class, m -> m.selectProductCategoryCount(productCategory));
    }

    @Override
    public boolean insertProductCategory(ProductCategory productCategory) {
        boolean insertFlag = SqlSessionFactoryTool.DoMapperAndCommit(ProductCategoryMapper.class, m -> m.insertProductCategory(productCategory));
        if (insertFlag) {
            JedisUtil.jedisDo(j -> j.del(PRODUCT_CATE_CACHE_KEY));
        }
        return insertFlag;
    }

    @Override
    public boolean updateProductCategory(ProductCategory productCategory) {
        boolean updateFlag = SqlSessionFactoryTool.DoMapperAndCommit(ProductCategoryMapper.class, m -> m.updateProductCategory(productCategory));
        if (updateFlag) {
            JedisUtil.jedisDo(j -> j.del(PRODUCT_CATE_CACHE_KEY));
        }
        return updateFlag;
    }

}
